package random;


import instrumentation.TrackerCoverage;


import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import GA.*;
import Operations.CreateObject;
import Operations.Operation;
import util.GAConfig;

public class Launcher {
	protected static Logger logger = Logger.getLogger("Random Generator");	
	
	private static  TrackerCoverage track=  TrackerCoverage.getTracker();
	
    private static long startTime;
    private static long elapsed;
private static final long TestTimeout = 60 * 500; /* Timeout at 30 seconds */
//private static final long TestTimeout = 60 * 1000 * 5; /* Timeout at 60 seconds */
	public static void main(String[] args) {
		System.out.println("main Random Test");
		try {
			run();
		} catch (Exception e) {
			logger.log(Level.SEVERE, "Something went wrong: " + e.getMessage(), e);
		}
		System.exit(0);
	}
	public static  void run() throws ClassNotFoundException {
	//	String cut="toyexamples.Stack";
	//	String cut="database.Driver";
		try {
			// get class from file

			List<String> classesToTest = ChromosomeFormer.readClassFromFile();
			// calculate the test cluster and the set of types involved in the
			TestClusterBuilder TestCluster = new TestClusterBuilder(
					classesToTest);
			Map<String, Class<?>> cluster = TestCluster.getCluster();
			Map<String, String[]> refFactory = TestCluster.getRefFactory();
//			Map<String, ConstructorDeclared[]> classToTest = TestCluster.getClassToTest();

			startTime = Calendar.getInstance().getTimeInMillis();
			elapsed = Calendar.getInstance().getTimeInMillis() - startTime;
			int it;
			for ( it = 2; (it <= 200000) && (elapsed < TestTimeout); it++) {
			   	Map<String, List<CreateObject>> constructors= new LinkedHashMap <String, List<CreateObject>> ();
		    	List<Operation> ops = new LinkedList<Operation>();
		    	constructors=CreateObject.generate(TestCluster);
				for (int i = 0; i < GAConfig.TestCaseRandomSize; i++)
					ops.add( Operation.randomlyGenerate(TestCluster));
			
				Chromosome chrom = new Chromosome(cluster,refFactory, constructors,ops);

//				System.out.println(chrom.toString()+" size:"+ops.size());
//				System.out.println("----------------------");
				ReflectionExecutor.execute(chrom);
//				 track.calculateFitness(chrom);

				
				elapsed = (Calendar.getInstance().getTimeInMillis() - startTime);
				if((elapsed%50)==0)
					System.out.println("elapsed time: "+ elapsed + " of "+TestTimeout);
			}
			System.out.println("Maximum test time exceeded at "+ elapsed + "ms: " + it + " iterations");
			
			track.summarizeCoverage("Random Generator");
			
			System.out.println(track.toString());
			System.out.println(track.displayCondition());
			System.out.println(" ** Done Testing! **");
		} catch (Exception e) {
			logger.log(Level.SEVERE, "Something went wrong: " + e.getMessage(), e);
		}
	}
	

}
